package tree;

public class ThreadsBinaryTree<T1,T2> {
    MyBinaryTreeNode<T1,T2> root;
    MyBinaryTreeNode<T1,T2> pre;
    public void add(T1 id,T2 element){
        MyBinaryTreeNode<T1,T2> node = new MyBinaryTreeNode<T1,T2>(id,element);
        if (root==null){
            root=node;
        }else {
            root.add(node,null);
        }
    }
    public void preOrderPrint(){
        if(root==null){
            return;
        }
        root.preOrderPrint();
    }

    public void postOrderPrint(){
        if(root==null){
            return;
        }
        root.postOrderPrint();
    }

    public void postOrderThreads(){
        if(root==null){
            return;
        }
        postOrderThread(root);
    }

    public void postOrderThread(MyBinaryTreeNode<T1,T2> node){
        if(node.getRight()!=null){
            postOrderThread(node.getRight());
        }
        if(node.getLeft()!=null){
            postOrderThread(node.getLeft());
        }
        if(node.getLeft()==null){
            node.setLeft(pre);
            node.setLeftPre(true);
        }
        if(pre!=null&&pre.getRight()==null){
            pre.setRight(node);
            pre.setRightPost(true);
        }
        pre = node;
    }
    public void postOrderThreadPrints(){
        postOrderThreadPrint(root);
    }
    public void postOrderThreadPrint(MyBinaryTreeNode<T1,T2> node){
        if(node==null){
            return;
        }
        if(node.getLeft()!=null){
            postOrderThreadPrint(node.getLeft());
        }
        node.printNode();
    }
    public T2 search(T1 id){
        if(root==null){
            return null;
        }
        return root.search(id);
    }
}
